﻿using System;
using Tiny.Imaging.Filter.PixelLibrary;

namespace Tiny.Imaging.Filter.PointFilter
{
    public class GammaFilter : BaseFilter.BaseFilter
    {
        private int[] _mapping;

        public GammaFilter(double factor = 0, Func<int, int, int> blend = null, int opacity = 100)
            : base(blend, opacity)
        {
            PrePopulate(factor);
        }

        public override int Process(Rgba rgba)
        {
            rgba.R = _mapping[rgba.R];
            rgba.G = _mapping[rgba.G];
            rgba.B = _mapping[rgba.B];

            return rgba.ToInteger();
        }

        private void PrePopulate(double factor)
        {
            _mapping = new int[256];

            for (int i = 0; i < 256; ++i)
            {
                _mapping[i] = Blend(i, (int)(Math.Pow(i / 255.0, factor) * 255));
            }
        }
    }
}